
package com.shiliao.framework.base.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.shiliao.framework.R;
import com.shiliao.framework.mvp.presenter.IBasePresenter;
import com.shiliao.framework.util.CommonUtil;
import com.shiliao.framework.util.StringUtils;
import com.shiliao.framework.util.ViewUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {


    public Activity mContext;
    protected LayoutInflater mInflater;
    protected ArrayList<T> mDataList;

    private IBasePresenter presenter;
    public static final int START_VIEWID = 99000000;

    public BaseAdapter(Activity context) {

        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mDataList = new ArrayList<T>();
    }

    private Map<Integer, Boolean> isFrist;
    private int mLastPos = 0;
    private boolean enableAnim = false;

    public void enableAnimation() {
        enableAnim = true;
        if (isFrist == null) {
            isFrist = new HashMap<Integer, Boolean>();
        }

    }

    public void setPresenter(IBasePresenter presenter) {
        this.presenter = presenter;
    }

    public List<T> getDataList() {
        return mDataList;
    }

    public void addDataList(List<T> list) {

        if (list != null && list.size() > 0) {
            mDataList.addAll(list);

            notifyDataSetChanged();

        }

    }

    public void addItem(T item) {
        if (item != null) {
            mDataList.add(item);
            notifyDataSetChanged();
        }

    }

    public void addHeadList(List<T> list) {
        if (list != null && list.size() > 0) {
            mDataList.addAll(0, list);
            notifyDataSetChanged();
        }

    }

    public void remove(int pos) {
        if (mDataList != null && mDataList.size() > pos) {
            // Object object = getItem(pos);
            // mDataList.remove(object);
            mDataList.remove(pos);
            notifyDataSetChanged();
        }

    }

    public void clear(boolean bRefresh) {

        if (mDataList != null) {
            mDataList.clear();
            if (bRefresh) {
                notifyDataSetChanged();
            }

        }

    }

    public void clear() {
        clear(true);

    }

    @Override
    public int getCount() {
        if (mDataList == null) {
            return 0;
        } else {
            return mDataList.size();
        }
    }

    @Override
    public T getItem(int pos) {
        if (mDataList == null || pos >= mDataList.size()) {
            return null;
        } else {
            return mDataList.get(pos);
        }
    }

    @Override
    public long getItemId(int pos) {
        // TODO Auto-generated method stub
        return pos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // SparseArray<View> holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    getViewLayout(), null);
        }

        updateView(position, convertView, false);

        if (isFrist != null && enableAnim) {
            if (position == 0 && isFrist != null) {
                isFrist.clear();
            }

            if (position < mLastPos) {
                isFrist.put(position, true);
            } else {
                if (isFrist.get(position) == null || isFrist.get(position)) {
                    Animation anim = AnimationUtils.loadAnimation(mContext,
                            R.anim.anim_transfer_bottom);
                    anim.setDuration((position % 10) * 60);
                    convertView.startAnimation(anim);
                    isFrist.put(position, false);
                }
            }
        }

        mLastPos = position;

        return convertView;
    }

    public abstract void updateView(int pos, View convertView, boolean bOut);

    public abstract int getViewLayout();

    public void loadImage(ImageView imageView, String url, int nDeafultImage) {

        if (presenter != null && imageView != null) {
            imageView.setTag(url);
            presenter.loadImageView(mContext,imageView, url, nDeafultImage);
        }
    }

    public static void setTextView(View convertView, int viewId, String text) {
        TextView textView = ViewUtils.getViewFromHolder(convertView, viewId);
        if (textView != null) {

            textView.setText(text);
        }

    }

    public void setImageView(View convertView, int viewId, String url,
                             int nDeafultImage) {

        ImageView imageView = ViewUtils.getViewFromHolder(convertView, viewId);

        if (imageView != null) {

            if (StringUtils.isNotEmpty(url)) {
                loadImage(imageView, url, nDeafultImage);
            } else {
                imageView.setImageResource(nDeafultImage);
            }

        }
    }

    public static void setImageView(View convertView, int viewId,
                                    int nDeafultImage) {

        ImageView imageView = ViewUtils.getViewFromHolder(convertView, viewId);

        if (imageView != null) {

            imageView.setImageResource(nDeafultImage);
        }
    }

    public static void hideView(View convertView, int viewId, boolean bHide) {

        View view = ViewUtils.getViewFromHolder(convertView, viewId);
        if (view != null) {

            view.setVisibility(bHide ? View.GONE : View.VISIBLE);
        }
    }

    //show short toast 方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 快捷显示short toast方法，需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
     *
     * @param stringResId
     */
    public void showShortToast(int stringResId) {
        CommonUtil.showShortToast(mContext, stringResId);
    }

    /**
     * 快捷显示short toast方法，需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
     *
     * @param string
     */
    public void showShortToast(String string) {
        CommonUtil.showShortToast(mContext, string);
    }
    //show short toast 方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //启动新Activity方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 打开新的Activity，向左滑入效果
     *
     * @param intent
     */
    public void toActivity(final Intent intent) {
        CommonUtil.toActivity(mContext, intent);
    }

    /**
     * 打开新的Activity
     *
     * @param intent
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final boolean showAnimation) {
        CommonUtil.toActivity(mContext, intent, showAnimation);
    }

    /**
     * 打开新的Activity，向左滑入效果
     *
     * @param intent
     * @param requestCode
     */
    public void toActivity(final Intent intent, final int requestCode) {
        CommonUtil.toActivity(mContext, intent, requestCode);
    }

    /**
     * 打开新的Activity
     *
     * @param intent
     * @param requestCode
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final int requestCode, final boolean showAnimation) {
        CommonUtil.toActivity(mContext, intent, requestCode, showAnimation);
    }
    //启动新Activity方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}
